namespace DotNetNuke.UI.WebControls
{
    using DotNetNuke.UI.Utilities;
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    internal class TreeNodeWriter : ITreeNodeWriter
    {
        private static readonly string[] _expcol = new string[] { "+", "-" };
        private TreeNode _Node;

        private string GetNodeCss(TreeNode oNode)
        {
            string cssClass = oNode.DNNTree.CssClass;
            if (oNode.Level > 0)
            {
                cssClass = oNode.DNNTree.DefaultChildNodeCssClass;
            }
            if (Strings.Len(oNode.CSSClass) > 0)
            {
                cssClass = oNode.CSSClass;
            }
            if (!oNode.Selected)
            {
                return cssClass;
            }
            if (Strings.Len(oNode.CSSClassSelected) > 0)
            {
                return (cssClass + " " + oNode.CSSClassSelected);
            }
            return (cssClass + " " + oNode.DNNTree.DefaultNodeCssClassSelected);
        }

        protected void Render(HtmlTextWriter writer)
        {
            this.RenderContents(writer);
            if (this._Node.HasNodes && this._Node.IsExpanded)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "Child");
                writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                this.RenderChildren(writer);
                writer.RenderEndTag();
            }
        }

        protected void RenderChildren(HtmlTextWriter writer)
        {
            IEnumerator enumerator=null;
            try
            {
                enumerator = this._Node.TreeNodes.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    ((TreeNode) enumerator.Current).Render(writer);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    ((IDisposable) enumerator).Dispose();
                }
            }
        }

        protected void RenderContents(HtmlTextWriter writer)
        {
            this.RenderOpenTag(writer);
            if ((this._Node.Level > 0) && (this._Node.DNNTree.IndentWidth > 0))
            {
                this.RenderSpacer(writer, this._Node.Level * this._Node.DNNTree.IndentWidth);
            }
            this.RenderExpandNodeIcon(writer);
            this.RenderNodeCheckbox(writer);
            this.RenderNodeIcon(writer);
            this.RenderNodeText(writer);
            writer.RenderEndTag();
        }

        protected void RenderExpandNodeIcon(HtmlTextWriter writer)
        {
            if (this._Node.HasNodes)
            {
                HyperLink link = new HyperLink();
                Image image = new Image();
                if (this._Node.IsExpanded)
                {
                    link.Text = _expcol[1];
                    if (Strings.Len(this._Node.DNNTree.ExpandedNodeImage) > 0)
                    {
                        image.ImageUrl = this._Node.DNNTree.ExpandedNodeImage;
                    }
                }
                else
                {
                    link.Text = _expcol[0];
                    if (Strings.Len(this._Node.DNNTree.CollapsedNodeImage) > 0)
                    {
                        image.ImageUrl = this._Node.DNNTree.CollapsedNodeImage;
                    }
                }
                link.NavigateUrl = ClientAPI.GetPostBackClientHyperlink(this._Node.DNNTree, this._Node.ID);
                if (Strings.Len(image.ImageUrl) > 0)
                {
                    link.RenderBeginTag(writer);
                    image.RenderControl(writer);
                    link.RenderEndTag(writer);
                }
                else
                {
                    link.RenderControl(writer);
                }
                image = null;
                link = null;
            }
            else
            {
                this.RenderSpacer(writer, 9);
            }
            writer.Write("&nbsp;");
        }

        public void RenderNode(HtmlTextWriter writer, TreeNode Node)
        {
            this._Node = Node;
            this.Render(writer);
        }

        protected void RenderNodeCheckbox(HtmlTextWriter writer)
        {
            if (this._Node.DNNTree.CheckBoxes)
            {
                CheckBox box = new CheckBox();
                box.ID = this._Node.ID + TreeNode._separator + TreeNode._checkboxIDSufix;
                box.Checked = this._Node.Selected;
                string expression = "";
                if (Strings.Len(this._Node.JSFunction) > 0)
                {
                    if (!this._Node.JSFunction.EndsWith(";"))
                    {
                        TreeNode node = this._Node;
                        node.JSFunction = node.JSFunction + ";";
                    }
                    expression = expression + this._Node.JSFunction;
                }
                if (Strings.Len(this._Node.DNNTree.JSFunction) > 0)
                {
                    if (!this._Node.DNNTree.JSFunction.EndsWith(";"))
                    {
                        DnnTree dNNTree = this._Node.DNNTree;
                        dNNTree.JSFunction = dNNTree.JSFunction + ";";
                    }
                    expression = expression + this._Node.DNNTree.JSFunction;
                }
                string str2 = ClientAPI.GetPostBackClientHyperlink(this._Node.DNNTree, this._Node.ID + ClientAPI.COLUMN_DELIMITER + "Click").Replace("javascript:", "") + ";";
                string str = ClientAPI.GetPostBackClientHyperlink(this._Node.DNNTree, this._Node.ID + ClientAPI.COLUMN_DELIMITER + "Checked").Replace("javascript:", "") + ";";
                if (!this._Node.Selected)
                {
                    if (Strings.Len(expression) > 0)
                    {
                        expression = ("if (eval(\"" + expression.Replace("\"", "\"\"") + "\") != false) ") + str2 + " else " + str;
                    }
                    else
                    {
                        expression = expression + str2;
                    }
                }
                else
                {
                    expression = str;
                }
                box.Attributes.Add("onclick", expression);
                box.RenderControl(writer);
                box = null;
                writer.Write("&nbsp;");
            }
        }

        protected void RenderNodeIcon(HtmlTextWriter writer)
        {
            Label label = new Label();
            if (Strings.Len(this._Node.CSSIcon) > 0)
            {
                label.CssClass = this._Node.CSSIcon;
            }
            else if (Strings.Len(this._Node.DNNTree.DefaultIconCssClass) > 0)
            {
                label.CssClass = this._Node.DNNTree.DefaultIconCssClass;
            }
            label.RenderBeginTag(writer);
            if (this._Node.ImageIndex > -1)
            {
                NodeImage image = this._Node.DNNTree.ImageList[this._Node.ImageIndex];
                if (image != null)
                {
                    Image image2 = new Image();
                    image2.ImageUrl = image.ImageUrl;
                    image2.RenderControl(writer);
                    writer.Write("&nbsp;");
                }
            }
            label.RenderEndTag(writer);
        }

        protected void RenderNodeText(HtmlTextWriter writer)
        {
            HyperLink link = new HyperLink();
            string expression = "";
            link.Text = this._Node.Text;
            if (Strings.Len(this._Node.JSFunction) > 0)
            {
                if (!this._Node.JSFunction.EndsWith(";"))
                {
                    TreeNode node = this._Node;
                    node.JSFunction = node.JSFunction + ";";
                }
                expression = expression + this._Node.JSFunction;
            }
            else if (Strings.Len(this._Node.DNNTree.JSFunction) > 0)
            {
                if (!this._Node.DNNTree.JSFunction.EndsWith(";"))
                {
                    DnnTree dNNTree = this._Node.DNNTree;
                    dNNTree.JSFunction = dNNTree.JSFunction + ";";
                }
                expression = expression + this._Node.DNNTree.JSFunction;
            }
            if (this._Node.Enabled)
            {
                switch (this._Node.ClickAction)
                {
                    case eClickAction.PostBack:
                    case eClickAction.Expand:
                        if (Strings.Len(expression) > 0)
                        {
                            expression = "if (eval(\"" + expression.Replace("\"", "\"\"") + "\") != false) ";
                        }
                        expression = expression + ClientAPI.GetPostBackClientHyperlink(this._Node.DNNTree, this._Node.ID + ClientAPI.COLUMN_DELIMITER + "Click").Replace("javascript:", "");
                        break;

                    case eClickAction.Navigate:
                        if (Strings.Len(expression) > 0)
                        {
                            expression = "if (eval(\"" + expression.Replace("\"", "\"\"") + "\") != false) ";
                        }
                        if (Strings.Len(this._Node.DNNTree.Target) > 0)
                        {
                            expression = expression + "window.frames." + this._Node.DNNTree.Target + ".location.href='" + this._Node.NavigateURL + "'; void(0);";
                        }
                        else
                        {
                            expression = expression + "window.location.href='" + this._Node.NavigateURL + "';";
                        }
                        break;
                }
                link.NavigateUrl = "javascript:" + expression;
            }
            if (Strings.Len(this._Node.ToolTip) > 0)
            {
                link.ToolTip = this._Node.ToolTip;
            }
            string nodeCss = this.GetNodeCss(this._Node);
            if (Strings.Len(nodeCss) > 0)
            {
                link.CssClass = nodeCss;
            }
            link.RenderControl(writer);
        }

        protected void RenderOpenTag(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this._Node.ID);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this._Node.ID.Replace(TreeNode._separator, "_"));
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
        }

        protected void RenderSpacer(HtmlTextWriter writer, int Width)
        {
            writer.AddStyleAttribute("width", Width.ToString() + "px");
            writer.AddStyleAttribute("height", "1px");
            writer.AddAttribute("src", this._Node.DNNTree.SystemImagesPath + "spacer.gif");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
        }
    }
}

